import { reg_gb2312_en, reg_mail, wechat_reg } from '@/utils/regs';
import { isValidName } from '@/utils/rules';
import Storage from '@/utils/Storage';

const key = `&&_APP_CURRENT_REGISTER_STEP`;

export const setCurrentStep = (index: number) => {
  Storage.set(key, index);
};

// 注册步骤，从 0 开始计算
export const getCurrentStep = () => {
  const index = Storage.get(key, 0);
  return Number(index);
};

export const step1Rules = {
  desiredPositionCode: {
    required: true,
    message: '请选择期望职位',
  },
  desiredIndustryCode: {
    required: true,
    message: '请选择期望行业',
  },
  currentState: {
    required: true,
    message: '请选择当前求职状态',
  },
  salary: [
    {
      required: true,
      trigger: 'change',
      validator: (_rule: any, value: any) => {
        if (value?.desiredSalaryMin > value?.desiredSalaryMax) {
          return Promise.reject('上限不能小于下限');
        }

        if (value?.desiredSalaryMax > value?.desiredSalaryMin * 2) {
          return Promise.reject('薪酬差不超过2倍');
        }
        if (!value?.desiredSalaryMin || !value?.desiredSalaryMax) {
          return Promise.reject('请输入薪酬范围');
        }
        return Promise.resolve();
      },
    },
  ],
};

// 完善信息rules
export const step2Rules = {
  // avatarUrl: {
  //   required: true,
  //   trigger: 'change',
  //   validator: (_rule: any, value: any) => {
  //     if (value?.some((t: any) => t.status === 'uploading')) {
  //       return Promise.resolve();
  //     }
  //     if (!value?.[0]?.response?.data) {
  //       return Promise.reject(`请上传头像`);
  //     }
  //     return Promise.resolve();
  //   },
  // },
  name: {
    required: true,
    validator: (_rule: any, value: any) => {
      if (isValidName(value)) {
        return Promise.resolve();
      }
      return Promise.reject(`输入10个汉字或字母，禁止输入特殊符号`);
    },
  },
  dateOfBirth: {
    required: true,
    message: '请选择出生日期',
    type: 'object',
  },
  workStartYear: {
    required: true,
    message: '请选择参加工作日期',
    type: 'object',
  },
  currentAreacode: {
    required: true,
    message: '请选择现居住地所在区',
  },
  currentStreetCode: {
    required: true,
    message: '请选择现居住地所在街道',
  },
  wechat: {
    required: false,
    trigger: 'change',
    validator: (rule: any, value: any) => {
      if (!value) {
        return Promise.resolve();
      } else {
        if (!wechat_reg.test(value)) {
          return Promise.reject(`请填写6-20位字母、数字、下划线或减号组合`);
        } else {
          return Promise.resolve();
        }
      }
    },
  },
  email: {
    required: false,
    trigger: 'change',
    validator: (rule: any, value: any) => {
      if (!value) {
        return Promise.resolve();
      }
      if (!reg_mail.test(value)) {
        return Promise.reject('请填写正确的邮箱');
      }
      return Promise.resolve();
    },
  },
  // detailedLocation: {
  //   required: true,
  //   validator(_rule: any, value: any) {
  //     if (!value?.address) {
  //       return Promise.reject(`请选择常住地址`);
  //     }
  //     return Promise.resolve();
  //   },
  // },
  salary: {
    required: true,
    validator(_rule: any, value: any) {
      if (!value?.salary || !value?.salaryMonth) {
        return Promise.reject(`当前薪资`);
      }
      return Promise.resolve();
    },
  },
};

export const step3Rules = {
  workExperienceDTO_companyName: {
    required: true,
    trigger: 'change',
    validator(rule: any, value: any) {
      if (!value || value?.length > 50 || typeof value !== 'string') {
        return Promise.reject(`请输入50个以内的字符`);
      }
      return Promise.resolve();
    },
  },

  workExperienceDTO_industryCode: {
    required: true,
    message: '请选择',
    trigger: 'change',
  },
  workExperienceDTO_positionCode: {
    required: true,
    message: '请选择',
    trigger: 'change',
  },
  workExperienceDTO_time: {
    required: true,
    message: '请选择在职时间',
    type: 'array',
    max: 2,
  },
  workExperienceDTO_description: {
    required: true,
    trigger: 'change',
    validator(rule: any, value: any) {
      if (
        value?.length < 20 ||
        value?.length > 150 ||
        typeof value !== 'string'
      ) {
        return Promise.reject(`限制20-150个字符`);
      }
      return Promise.resolve();
    },
  },
  projectExperienceDTO_projectName: {
    required: true,
    trigger: 'change',
    validator(rule: any, value: any) {
      if (!value || value?.length > 16 || typeof value !== 'string') {
        return Promise.reject(`请输入16个以内的字符`);
      }
      return Promise.resolve();
    },
  },
  projectExperienceDTO_time: {
    required: true,
    trigger: 'change',
    type: 'array',
    len: 2,
    message: '请选择项目时间',
  },

  projectExperienceDTO_jobTitle: {
    required: true,
    trigger: 'change',
    validator(rule: any, value: any) {
      if (!value || !reg_gb2312_en(16).test(value)) {
        return Promise.reject(`请输入16个汉字或字母`);
      }
      return Promise.resolve();
    },
  },
  projectExperienceDTO_description: {
    required: true,
    trigger: 'change',
    validator(rule: any, value: any) {
      if (
        value?.length < 20 ||
        value?.length > 150 ||
        typeof value !== 'string'
      ) {
        return Promise.reject(`限制20-150个字符`);
      }
      return Promise.resolve();
    },
  },
};

// 教育经历 rules
export const step4Rules = {
  schoolName: {
    required: true,
    trigger: 'blur',
    validator(rule: any, value: any) {
      if (!value || !reg_gb2312_en(50).test(value)) {
        return Promise.reject(`限制50个以内的汉字和字母`);
      }
      return Promise.resolve();
    },
  },

  degreeCode: {
    required: true,
    trigger: 'change',
    message: '请选择学历',
  },
  major: {
    required: true,
    trigger: 'blur',
    validator(rule: any, value: any) {
      if (!value || !reg_gb2312_en(50).test(value)) {
        return Promise.reject(`限制50个以内的汉字和字母`);
      }
      return Promise.resolve();
    },
  },
  time: {
    required: true,
    message: '请选择时间段',
    type: 'array',
    len: 2,
  },
};
